package cn.bdqn.sys.service.impl;

import cn.bdqn.model.EUList;
import cn.bdqn.model.PageBean;
import cn.bdqn.model.R;
import cn.bdqn.service.BaseService;
import cn.bdqn.sys.mapper.RoleMapper;
import cn.bdqn.sys.mapper.RoleResourcesMapper;
import cn.bdqn.sys.model.Role;
import cn.bdqn.sys.model.RoleExample;
import cn.bdqn.sys.model.RoleResourcesExample;
import cn.bdqn.sys.service.RoleService;
import cn.bdqn.sys.vomapper.RoleVoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gurilla
 * @create 2019-08-04 10:30
 */
@Service
public class RoleServiceImpl  extends BaseService implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public EUList<Role> list(PageBean pageBean, String rolename) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIsdelEqualTo(false);
        criteria.andEidEqualTo(getEid());   //从session来
        criteria.andIsadminEqualTo(false);
        if(!StringUtils.isEmpty(rolename)){
            criteria.andRnameLike("%"+rolename+"%");
        }
        PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        List<Role> roles = roleMapper.selectByExample(example);
        PageInfo<Role> info=new PageInfo<>(roles);
        return new EUList<>(info.getTotal(),info.getList());
    }

    @Override
    public R save(Role role) {
//        try{
            if(role.getRid()==null){
                role.setEid(getEid());
                role.setOpid(getUser().getAccount());
                roleMapper.insertSelective(role);
            }else{
                roleMapper.updateByPrimaryKeySelective(role);
            }
            return R.ok();
//        }catch (Exception e){
//            e.printStackTrace();
//            return R.error("保存失败");
//        }
    }

    @Override
    public R delete(Integer roleid) {
        Role role = new Role();
        role.setRid(roleid);
        role.setOpid(getUser().getAccount());
        role.setIsdel(true);
        try{
            roleMapper.updateByPrimaryKeySelective(role);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return  R.error("删除失败");
        }
    }
//多表操作时必须增加transactional以示事务型
    @Resource
    private RoleVoMapper roleVoMapper;

    @Resource
    private RoleResourcesMapper roleResourcesMapper;

    @Transactional
    @Override
    public R insertResRole(Integer roleid, String resids) {
        try{
            RoleResourcesExample example = new RoleResourcesExample();
            example.createCriteria().andRoleIdEqualTo((long)roleid);
            roleResourcesMapper.deleteByExample(example);
            roleVoMapper.insertResRole(roleid,resids,getEid());
            return R.ok();
        }
        catch(Exception e){
           e.printStackTrace();
           return R.error("权限设置失败");
            }
    }

    @Override
    public R getall() {
//        try {
            RoleExample example=new RoleExample();
            example.createCriteria().andEidEqualTo(getEid())
                    .andIsdelEqualTo(false)
                    .andIsadminEqualTo(false);

            List<Role> roles = roleMapper.selectByExample(example);
            return R.ok(null, roles);
//        }catch (Exception e){
//            e.printStackTrace();
//            return R.error("查询失败！");
//        }
    }
}
